<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/10 16:56
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use think\facade\View;

class Index extends Common
{
    public function index()
    {
        $menu_model = new \app\common\model\Menu();
        $menu_list = $menu_model->getList();
        if ($this->adminData['role_id'] != 1) {
            if ($this->adminData['menu_list']) {
                $auth_menu = [];
                foreach ($menu_list as $key=>$val){
                    if ($val['parent_id'] == 0) {
                        $subclass = [];
                        foreach ($menu_list as $kk => $vv) {
                            if ($vv['parent_id'] == $val['menu_id']) {
                                if (in_array($vv['menu_id'], $this->adminData['menu_list'])) {
                                    $sub_class = [];
                                    foreach ($menu_list as $k => $v) {
                                        if ($v['parent_id'] == $vv['menu_id']) {
                                            if (in_array($v['menu_id'], $this->adminData['menu_list'])) {
                                                $sub_class[] = $v;
                                            }
                                        }
                                    }
                                    $vv['subclass'] = $sub_class;
                                    $subclass[] = $vv;
                                }
                            }
                        }
                        $val['subclass'] = $subclass;
                        if (!empty($subclass)){
                            $auth_menu[] = $val;
                        }
                    }
                }
                $menu_list = $auth_menu;
            }else {
                $menu_list = [];
            }
        }else{
            $auth_menu = [];
            foreach ($menu_list as $key=>$val){
                if ($val['parent_id'] == 0) {
                    $subclass = [];
                    foreach ($menu_list as $kk => $vv) {
                        if ($vv['parent_id'] == $val['menu_id']) {
                            $sub_class = [];
                            foreach ($menu_list as $k => $v) {
                                if ($v['parent_id'] == $vv['menu_id']) {
                                    $sub_class[] = $v;
                                }
                            }
                            $vv['subclass'] = $sub_class;
                            $subclass[] = $vv;
                        }
                    }
                    $val['subclass'] = $subclass;
                    if (!empty($subclass)){
                        $auth_menu[] = $val;
                    }
                }
            }
            $menu_list = $auth_menu;
        }

        View::assign('menu_list', $menu_list);
        return View::fetch();
    }

    public function main()
    {
        /*if (in_array($this->adminData['role_id'], [1,2,3])) {
            // 代理商人数
            $admin_model = new \app\common\model\Admin();
            if (!empty($this->area_id)){
                $admin_list = $admin_model->getList(['admin_pid'=>$this->adminId], '', '', 'admin_id,area_ids');
                $business_count = 0;
                if (!empty($admin_list)){
                    if (is_array($admin_list)){
                        foreach ($admin_list as $key=>$val){
                            $area_array = explode(',', $val['area_ids']);
                            if (in_array($this->area_id, $area_array)){
                                $business_count++;
                            }
                        }
                    }
                }
            }else{
                if ($this->adminId != 1){
                    $business_count = $admin_model->where(['admin_pid'=>$this->adminId])->count();
                }else{
                    $business_count = $admin_model->where(['role_id'=>2])->count();
                }
            }
            // 车辆总数量
            $vehicle_model = new \app\common\model\Vehicle();
            if (!empty($this->area_id)){
                $vehicle_count = $vehicle_model->where('area_id', $this->area_id)->count();
            }else{
                if ($this->adminId != 1){
                    $vehicle_count = $vehicle_model->where('admin_id', $this->adminId)->count();
                }else{
                    $vehicle_count = $vehicle_model->count();
                }
            }
            // 正在运营车辆数量
            if (!empty($this->area_id)){
                $vehicle_start = $vehicle_model->where(['state'=>1,'area_id'=>$this->area_id])->count();
            }else{
                if ($this->adminId != 1){
                    $vehicle_start = $vehicle_model->where('admin_id', $this->adminId)->count();
                }else{
                    $vehicle_start = $vehicle_model->where(['state'=>1])->count();
                }
            }
            // 获取今日数据
            $member_finance = new MemberFinance();
            $day_time = strtotime(date('Y-m-d') . ' 00:00:00');
            $finance_where['create_time'] = ['egt', $day_time];
            if (!empty($this->area_id)){
                $finance_where['area_id'] = $this->area_id;
            }else{
                if ($this->adminId != 1){
                    $finance_where['area_id'] = ['in', $this->adminData['area_ids']];
                }
            }
            $finance_where['is_payment'] = 1;
            // 今日消费
            $finance_where['type'] = ['in', '3,5'];
            $consume = $member_finance->where($finance_where)->sum('money');
            // 今日消费退款
            $finance_where['type'] = ['in', '6,41'];
            $refund = $member_finance->where($finance_where)->sum('money');
            // 今日收入
            $income = $consume + $refund;
            // ==================
            // 低电车辆
            if (!empty($this->area_id)){
                $low_voltage = $vehicle_model->where(['area_id'=>$this->area_id,'state'=>1,'low_voltage'=>1])->count();
            }else{
                if ($this->adminId != 1){
                    $low_voltage = $vehicle_model->where(['area_id'=>['in', $this->adminData['area_ids']],'state'=>1,'low_voltage'=>1])->count();
                }else{
                    $low_voltage = $vehicle_model->where(['state'=>1,'low_voltage'=>1])->count();
                }
            }
            // 24小时未骑车辆
            $last_time = NEW_TIME - (3600 * 24);
            if (!empty($this->area_id)){
                $overtime = $vehicle_model->where(['area_id'=>$this->area_id,'state'=>1,'status'=>0,'last_time'=>['lt', $last_time]])->count();
            }else{
                if ($this->adminId != 1){
                    $overtime = $vehicle_model->where(['area_id'=>['in', $this->adminData['area_ids']],'status'=>0,'last_time'=>['lt', $last_time]])->count();
                }else{
                    $overtime = $vehicle_model->where(['state'=>1,'status'=>0,'last_time'=>['lt', $last_time]])->count();
                }
            }
            // 24小时未还车辆
            if (!empty($this->area_id)){
                $unfinished = $vehicle_model->where(['area_id'=>$this->area_id,'state'=>1,'status'=>['in', '1,2'],'last_time'=>['lt', $last_time]])->count();
            }else{
                if ($this->adminId != 1){
                    $unfinished = $vehicle_model->where(['area_id'=>['in', $this->adminData['area_ids']],'state'=>1,'status'=>['in', '1,2'],'last_time'=>['lt', $last_time]])->count();
                }else{
                    $unfinished = $vehicle_model->where(['state'=>1,'status'=>['in', '1,2'],'last_time'=>['lt', $last_time]])->count();
                }
            }
            // 故障车辆
            if (!empty($this->area_id)){
                $malfunction = $vehicle_model->where(['area_id'=>$this->area_id,'status'=>4])->count();
            }else{
                if ($this->adminId != 1){
                    $malfunction = $vehicle_model->where(['area_id'=>['in', $this->adminData['area_ids']],'status'=>4])->count();
                }else{
                    $malfunction = $vehicle_model->where(['status'=>4])->count();
                }
            }
            // 报警信息
            $alarm_log = new AlarmLog();
            if (!empty($this->area_id)){
                $alarm = $alarm_log->where('area_id', $this->area_id)->count();
            }else{
                if ($this->adminId != 1){
                    $alarm = $alarm_log->where(['area_id'=>['in', $this->adminData['area_ids']]])->count();
                }else{
                    $alarm = $alarm_log->count();
                }
            }
            // 反馈内容
            $order_feedback = new \app\common\model\OrderFeedback();
            if (!empty($this->area_id)){
                $feedback_count = $order_feedback->where(['area_id'=>$this->area_id,'state'=>0])->count();
            }else{
                if ($this->adminId != 1){
                    $feedback_count = $order_feedback->where(['area_id'=>['in', $this->adminData['area_ids']],'state'=>0])->count();
                }else{
                    $feedback_count = $order_feedback->where(['state'=>0])->count();
                }
            }
            // ==================
            // 财务统计
            if (!empty($this->area_id)){
                $this->fundArea();
            }else{
                if ($this->adminId != 1){
                    $this->fund();
                }else{
                    $this->fund();
                }
            }

            $this->assign([
                'business_count'=>$business_count,
                'vehicle_count'=>$vehicle_count,
                'vehicle_start'=>$vehicle_start,
                'consume'=>empty($consume) ? 0 : $consume,
                'refund'=>empty($refund) ? 0 : $refund,
                'income'=>empty($income) ? 0 : $income,
                'low_voltage'=>$low_voltage,
                'overtime'=>$overtime,
                'unfinished'=>$unfinished,
                'malfunction'=>$malfunction,
                'alarm'=>$alarm,
                'feedback_count'=>$feedback_count,
            ]);

            if ($this->area_id){
                return $this->fetch('main_area');
            }else{
                if ($this->adminId != 1){
                    return $this->fetch('main_area');
                }else{
                    return $this->fetch();
                }
            }
        }else{
            $this->redirect('Vehicle/index');
            return false;
        }*/
        return View::fetch();
    }

    public function fundArea()
    {
        $xAxis = [
            date('Y-m-d', strtotime('-7 day')),
            date('Y-m-d', strtotime('-6 day')),
            date('Y-m-d', strtotime('-5 day')),
            date('Y-m-d', strtotime('-4 day')),
            date('Y-m-d', strtotime('-3 day')),
            date('Y-m-d', strtotime('-2 day')),
            date('Y-m-d', strtotime('-1 day'))
        ];
        // table
        $color = ['#6dcac9', '#ff6d61'];
        $legend = ['订单数量', '订单收入'];
        // order
        $order_number = [];
        // consume
        $consume = [];
        $balance_log = new BalanceLog();
        foreach ($xAxis as $key=>$val){
            $beg = strtotime($val . ' 00:00:00');
            // 获取之前数据
            $log_where['create_time'] = $beg;
            $log_where['area_id'] = $this->area_id;
            $log_data = $balance_log->getFind($log_where,'order_count,total,buy_card');
            if (!empty($log_data)){
                $order_number[] = intval($log_data['order_count']);
                $consume[] = round(floatval($log_data['total']), 2);
            }else{
                $order_number[] = 0;
                $consume[] = '0.00';
            }
        }
        // 今日订单数量
        // 获取当前时间
        $start_time = date('Y-m-d', NEW_TIME);
        $beg = strtotime($start_time . ' 00:00:00');
        $end = strtotime($start_time . ' 23:59:59');
        // 今日订单数量
        if (!empty($this->area_id)) {
            $order_where['area_id'] = $this->area_id;
        }
        $order_where['start_time'] = [['egt', $beg],['elt', $end]];
        $order_model = new Order();
        $order_count = $order_model->where($order_where)->count();
        // 今日收益
        $day_earnings = '0.00';
        if (!empty($this->areaData)){
            $log_data = $balance_log->get_time_logs($this->areaData['admin_id'], $this->areaData['area_id']);
            if (!empty($log_data['0']['total'])){
                $day_earnings = floatval($log_data['0']['total']);
            }
        }
        // 今日换电次数
        if (!empty($this->area_id)) {
            $charging_where['area_id'] = $this->area_id;
        }
        $charging_where['create_time'] = [['egt', $beg], ['elt', $end]];
        $charging = new Charging();
        $charging_count = $charging->where($charging_where)->count();
        // 今日调度次数
        if (!empty($this->area_id)) {
            $move_where['area_id'] = $this->area_id;
        }
        $move_where['create_time'] = [['egt', $beg], ['elt', $end]];
        $dispatch_model = new Dispatch();
        $move_count = $dispatch_model->where($move_where)->count();

        $this->assign([
            'color'=>json_encode($color),
            'legend'=>json_encode($legend),
            'xAxis'=>json_encode($xAxis),
            'fund_order_number'=>json_encode($order_number),
            'fund_consume'=>json_encode($consume),
            'order_count'=>$order_count,
            'day_earnings'=>$day_earnings,
            'charging_count'=>$charging_count,
            'move_count'=>$move_count,
        ]);
    }

    public function fund()
    {
        $xAxis = [
            date('Y-m-d', strtotime('-7 day')),
            date('Y-m-d', strtotime('-6 day')),
            date('Y-m-d', strtotime('-5 day')),
            date('Y-m-d', strtotime('-4 day')),
            date('Y-m-d', strtotime('-3 day')),
            date('Y-m-d', strtotime('-2 day')),
            date('Y-m-d', strtotime('-1 day'))
        ];
        if ($this->adminData['role_id'] == 1) {
            // table
            $color = ['#6dcac9', '#58c8f2'];
            $legend = ['订单消费','订单退款'];
            // consume
            $consume = [];
            // refund
            $refund = [];
            if (!empty($xAxis)){
                $finance_log = new FinanceLog();
                foreach ($xAxis as $key=>$val){
                    $beg = strtotime($val . ' 00:00:00');
                    $end = strtotime($val . ' 23:59:59');
                    $list_where['create_time'] = [['egt', $beg], ['elt', $end]];
                    $data = $finance_log->logList($list_where);
                    if (!empty($data)){
                        if (is_array($data)){
                            foreach ($data as $k=>$v){
                                $consume[] = $v['consume'];
                                $refund[] = $v['consume_refund'];
                            }
                        }
                    }else{
                        $consume[] = '0.00';
                        $refund[] = '0.00';
                    }
                }
            }
            $this->assign([
                'color'=>json_encode($color),
                'legend'=>json_encode($legend),
                'xAxis'=>json_encode($xAxis),
                'fund_consume'=>json_encode($consume),
                'fund_refund'=>json_encode($refund),
            ]);

        }else if ($this->adminData['role_id'] == 2 || $this->adminData['role_id'] == 3){
            $xAxis = [
                date('Y-m-d', strtotime('-7 day')),
                date('Y-m-d', strtotime('-6 day')),
                date('Y-m-d', strtotime('-5 day')),
                date('Y-m-d', strtotime('-4 day')),
                date('Y-m-d', strtotime('-3 day')),
                date('Y-m-d', strtotime('-2 day')),
                date('Y-m-d', strtotime('-1 day'))
            ];
            // table
            $color = ['#6dcac9', '#ff6d61'];
            $legend = ['订单', '收益'];
            // order
            $order_number = [];
            // consume
            $consume = [];
            $balance_log = new BalanceLog();
            foreach ($xAxis as $key=>$val){
                $beg = strtotime($val . ' 00:00:00');
                // 获取之前数据
                $log_where['create_time'] = $beg;
                $log_where['area_id'] = ['in', $this->adminData['area_ids']];
                // $log_data = $balance_log->getFind($log_where,'order_count,total,buy_card');
                $order_number[] = $balance_log->where($log_where)->sum('order_count');
                $total = $balance_log->where($log_where)->sum('total');
                $consume[] = round(floatval($total), 2);
            }
            // 今日订单数量
            // 获取当前时间
            $start_time = date('Y-m-d', NEW_TIME);
            $beg = strtotime($start_time . ' 00:00:00');
            $end = strtotime($start_time . ' 23:59:59');
            // 今日订单数量
            if (!empty($this->area_id)) {
                $order_where['area_id'] = $this->area_id;
            }else{
                $order_where['area_id'] = ['in', $this->adminData['area_ids']];
            }
            $order_where['start_time'] = [['egt', $beg],['elt', $end]];
            $order_model = new Order();
            $order_count = $order_model->where($order_where)->count();
            // 今日收益
            $day_earnings = '0.00';
            if (!empty($this->areaData)){
                $log_data = $balance_log->get_time_logs($this->adminId, $this->areaData['area_ids']);
                if (!empty($log_data['0']['total'])){
                    $day_earnings = floatval($log_data['0']['total']);
                }
            }
            // 今日换电次数
            $charging_where['create_time'] = [['egt', $beg], ['elt', $end]];
            if (!empty($this->area_id)) {
                $charging_where['area_id'] = $this->area_id;
            }else{
                $charging_where['area_id'] = ['in', $this->adminData['area_ids']];
            }
            $charging = new Charging();
            $charging_count = $charging->where($charging_where)->count();
            // 今日调度次数
            $move_where['create_time'] = [['egt', $beg], ['elt', $end]];
            if (!empty($this->area_id)) {
                $move_where['area_id'] = $this->area_id;
            }else{
                $move_where['area_id'] = ['in', $this->adminData['area_ids']];
            }
            $dispatch_model = new Dispatch();
            $move_count = $dispatch_model->where($move_where)->count();

            $this->assign([
                'color'=>json_encode($color),
                'legend'=>json_encode($legend),
                'xAxis'=>json_encode($xAxis),
                'fund_order_number'=>json_encode($order_number),
                'fund_consume'=>json_encode($consume),
                'order_count'=>$order_count,
                'day_earnings'=>$day_earnings,
                'charging_count'=>$charging_count,
                'move_count'=>$move_count,
            ]);
        }
    }

    protected function getFund($type, $param, $payment_mode = false)
    {
        $list_where['type'] = $type;
        $list_where['is_payment'] = 1;
        $list_where['payment_mode'] = ['in', '1,2,3,4,5,6'];
        // 1余额，2微信，3支付宝，4银行卡，5微信小程序，6支付宝小程序
        if ($payment_mode){
            $list_where['payment_mode'] = $payment_mode;
        }
        $beg = strtotime($param . ' 00:00:00');
        $end = strtotime($param . ' 23:59:59');
        if ($type == 9){
            $list_where['update_time'] = [['egt', $beg], ['elt', $end]];
        }else{
            $list_where['create_time'] = [['egt', $beg], ['elt', $end]];
        }
        $member_finance = new MemberFinance();
        $money = $member_finance->where($list_where)->sum('money');
        return round($money, 2);
    }

}